using System;
using System.Collections.Generic;
using UnityEngine;

public class MapListDisplayController : ViewController , IMapListPanel{
 
    public MapListPanel panel;
    MapListPanelData m_Data;
    public MapListPanelData data => m_Data;

    private MapListItem curSelectedItem;

    private const string LastRequestMapListTimeKey = "LastRequestMapListTimeKey";
    public static double lastRequestMapListTime
    {
        get
        {
             double.TryParse(PlayerPrefs.GetString(LastRequestMapListTimeKey),out double value);
             return value;
        }
        set{PlayerPrefs.SetString(LastRequestMapListTimeKey,value.ToString());}
    }


    public void Init(MapListPanelData MapListPanelData)
    {
        m_Data = MapListPanelData;
        
        if(panel != null)
            panel.Init(m_Data, this);
    }

    
    protected override void WillLoadView()
    {
        if (panel == null)
        {
            var pref = ResourceManager.Load<MapListPanel>(ViewPath.MapListPanel);
            panel = Instantiate(pref, transform);
            panel.layer = 5;
        }

        panel.Init(m_Data,this);
    }

    public override void WillHideView()
    {
        base.WillHideView();
        
        this.panel.PlayHideAnim();
    }

    public override void OnViewDelete()
    {
        base.OnViewDelete();

        if (this.panel)
        {
            Destroy(panel.gameObject);
        }
        panel = null;
    }

    private void OnDestroy()
    {
        m_Data = null;
    }

    public void OnSelectItem(MapListItem item)
    {
        //Game.Instance.user.UpdateLevel(item.level);
        Game.s_CurLevel = item.level;

        Game.Instance.LoadGame(item.level);
    }

    public virtual void OnClickGoBack()
    {
        GameDebug.Log("OnClickGoBack");
        this.parentNav.LoadDisplay<BattleUIViewController>();
    }
}